package cn.snmp.mutual.nms.mibs;

import java.io.*;
import java.util.*;

public class LeafSyntax implements Serializable, MIBConstants, Cloneable {
    protected Hashtable m_propreties;
    RangeList range;

    public LeafSyntax(final Hashtable propreties) {
        this.m_propreties = null;
        this.range = null;
        this.m_propreties = propreties;
        if (this.isRanged()) {
            this.range = new RangeList((Hashtable) this.m_propreties.get("Range"));
        }
        if (this.isEnumerated()) {
            this.range = new RangeList((Hashtable) this.m_propreties.get("Enumerate"));
        }
    }

    public Object clone() throws CloneNotSupportedException {
        final Hashtable htPropreties = (Hashtable) this.m_propreties.clone();
        return new LeafSyntax(htPropreties);
    }

    @Override
    protected void finalize() throws Throwable {
        this.m_propreties = null;
        super.finalize();
    }

    public byte getType() {
        final String type = (String) this.m_propreties.get("Type");
        return Byte.parseByte(type);
    }

    public String getName() {
        return this.toString();
    }

    public String getEquivname() {
        return (String) this.m_propreties.get("Equivname");
    }

    @Override
    public String toString() {
        return (String) this.m_propreties.get("String");
    }

    public String toTagString() {
        if (this.range != null) {
            return this.getName() + " " + this.range.toString();
        }
        return this.getName();
    }

    public long getMax() {
        return Long.parseLong((String) this.m_propreties.get("Max"));
    }

    public long getMin() {
        return Long.parseLong((String) this.m_propreties.get("Min"));
    }

    public boolean isEnumerated() {
        final String proprety = (String) this.m_propreties.get("isEnumerated");
        return proprety != null && proprety.equalsIgnoreCase("true");
    }

    public String[] getEnumlabels() {
        final Vector labelVector = new Vector();
        final Hashtable table = (Hashtable) this.m_propreties.get("Enumerate");
        final Enumeration e = table.keys();
        while (e.hasMoreElements()) {
            labelVector.add(e.nextElement());
        }
        final String[] labels = new String[labelVector.size()];
        for (int i = 0; i < labels.length; ++i) {
            labels[i] = (String) labelVector.get(i);
        }
        return labels;
    }

    public int[] getEnumint() {
        final Vector labelVector = new Vector();
        final Hashtable table = (Hashtable) this.m_propreties.get("Enumerate");
        final Enumeration e = table.keys();
        while (e.hasMoreElements()) {
            labelVector.add(table.get(e.nextElement()));
        }
        final int[] labels = new int[labelVector.size()];
        for (int i = 0; i < labels.length; ++i) {
            labels[i] = Integer.parseInt((String) labelVector.get(i));
        }
        return labels;
    }

    public boolean isRanged() {
        final String proprety = (String) this.m_propreties.get("isRanged");
        return proprety != null && proprety.equalsIgnoreCase("true");
    }

    public String[] getRangeItems() {
        final Vector v = (Vector) this.m_propreties.get("Range");
        final String[] rangeItems = new String[v.size()];
        for (int i = 0; i < rangeItems.length; ++i) {
            rangeItems[i] = (String) v.get(i);
        }
        return rangeItems;
    }
}
